/**
 * https://leetcode-cn.com/problems/range-sum-query-2d-immutable/submissions/
 * @param {number[][]} matrix
 */
var NumMatrix = function(matrix) {
    var m = matrix.length;
    var n = matrix[0].length;
    this.arr = new Array(m+1).fill(0).map(() => new Array(n+1).fill(0));
    for(let i=1;i<=m;i++){
        for(let j=1;j<=n;j++){
            this.arr[i][j] = this.arr[i-1][j] + this.arr[i][j-1] - this.arr[i-1][j-1] + matrix[i-1][j-1];
        }
    }
};

/** 
 * @param {number} row1 
 * @param {number} col1 
 * @param {number} row2 
 * @param {number} col2
 * @return {number}
 */
NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) {
    return this.arr[row2+1][col2+1] - this.arr[row1][col2+1] - this.arr[row2+1][col1] + this.arr[row1][col1];
};